package com.irun.pm.controller;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.irun.pm.database.DBHelper;
import com.irun.pm.interfaces.IEnforcementController;
import com.irun.pm.mode.Enforcement;
import com.irun.pm.utils.Utils;

/***
 * @author huangsm
 * @date 2012-5-26
 * @email huangsanm@gmail.com
 * @version 1.0
 * @description 现场执法实现类
 */
public class EnforcementManager implements IEnforcementController {

	private DBHelper mDBHelper;
	public EnforcementManager(Context context){
		mDBHelper = new DBHelper(context);
		try {
			mDBHelper.createDataBase();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 现场执法，查询列表
	 * @author huangsm
	 * @date 2012-5-27
	 * @email huangsanm@gmail.com
	 * @description
	 */
	public List<Enforcement> getEnforcementList(String name, String date_from, String date_to, String type){
		List<Enforcement> enforcements = new ArrayList<Enforcement>();
		/*Cursor cursor = null;
		SQLiteDatabase db = null;
		try {
			String[] columns = {DBHelper.FIELD_ID, DBHelper.FIELD_NAME, 
					DBHelper.FIELD_DATE, DBHelper.FIELD_ADDRESS, DBHelper.FIELD_PATH, DBHelper.FIELD_NATIVE , DBHelper.FIELD_CARD, DBHelper.FIELD_NOTE};
			String selection = " type=" + type;
			if(!StringUtil.isEmpty(name)){
				selection += " and " + DBHelper.FIELD_NATIVE + " like '%" + name + "%'";
			}
			if(!StringUtil.isEmpty(date_to)){
				selection += " and " + DBHelper.FIELD_DATE + "<='" + date_to + "'";
			}
			if(!StringUtil.isEmpty(date_from)){
				selection += " and " + DBHelper.FIELD_DATE + ">='" + date_from + "'";
			}
			//String[] selectionArgs = {type, name, date_from, date_to};
			db = mDBHelper.getReadableDatabase();
			cursor = db.query(DBHelper.TABLE_DRIVERSINFO, columns, 
					selection, null, null, null, DBHelper.FIELD_ID + " desc");
			while (cursor.moveToNext()) {
				Enforcement ent = new Enforcement(
						cursor.getInt(cursor.getColumnIndex(DBHelper.FIELD_ID)), 
						cursor.getString(cursor.getColumnIndex(DBHelper.FIELD_NAME)), 
						cursor.getString(cursor.getColumnIndex(DBHelper.FIELD_DATE)), 
						cursor.getString(cursor.getColumnIndex(DBHelper.FIELD_ADDRESS)), 
						cursor.getString(cursor.getColumnIndex(DBHelper.FIELD_PATH)),
						cursor.getString(cursor.getColumnIndex(DBHelper.FIELD_CARD)),
						cursor.getString(cursor.getColumnIndex(DBHelper.FIELD_NATIVE)),
						cursor.getString(cursor.getColumnIndex(DBHelper.FIELD_NOTE))
						);
				enforcements.add(ent);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(cursor != null){
				cursor.close();
				db.close();
			}
		}*/
		return enforcements;
	}
	
	
	/***
	 * 添加现场执法
	 * @author huangsm
	 * @date 2012-5-27
	 * @email huangsanm@gmail.com
	 * @description
	 */
	public long saveEnforcement(Enforcement enf){
		SQLiteDatabase db = null;
		try{
			ContentValues cv = new ContentValues();
			cv.put(DBHelper.FIELD_NAME, enf.getName());
			cv.put(DBHelper.FIELD_DATE, enf.getDate());
			cv.put(DBHelper.FIELD_ADDRESS, enf.getPlace());
			cv.put(DBHelper.FIELD_PATH, enf.getPhoto());
			cv.put(DBHelper.FIELD_TYPE, Utils.TYPE_SITE_QUERY);
			cv.put(DBHelper.FIELD_NOTE, enf.getNotes());
			cv.put(DBHelper.FIELD_CARD, enf.getCar());
			db = mDBHelper.getWritableDatabase();
			long result = db.insert(DBHelper.TABLE_DRIVERSINFO, null, cv);
			
			return result;
		}catch (Exception e) {
			return -1;
		}finally{
			db.close();
		}
	}

	@Override
	public Enforcement getEnforcementById(int id) {
		
		return null;
	}
	
	
}
